package com.lv.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lv.mapper.TbItemCatMapper;
import com.lv.mapper.TbTypeTemplateMapper;
import com.lv.model.TbItemCat;
import com.lv.model.TbTypeTemplate;
import com.lv.model.TbTypeTemplateExample;
import com.lv.service.TypeService;
import com.lv.vo.TypeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {
    @Resource
    private TbTypeTemplateMapper tm;
    
    //查询

    @Override
    public List<TypeVo> getType(TypeVo vo) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNoneBlank(vo.getName())){
            criteria.andNameLike("%"+vo.getName()+"%");
        }
        List<TbTypeTemplate> tbTypeTemplates = tm.selectByExample(example);
        List<TypeVo> typeVos = JSONObject.parseArray(JSONObject.toJSONString(tbTypeTemplates), TypeVo.class);
        return typeVos;
    }

    //增加

    @Override
    public void insertType(TypeVo vo) {
        TbTypeTemplate type = new TbTypeTemplate();
        BeanUtils.copyProperties(vo, type);
        tm.insertSelective(type);
    }

    //删除
    @Override
    public void deleteType(List<Long> ids) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        tm.deleteByExample(example);
    }

    //回显

    @Override
    public TypeVo toUpdate(Long id) {
        TbTypeTemplate tbTypeTemplate = tm.selectByPrimaryKey(id);
        TypeVo vo = new TypeVo();
        BeanUtils.copyProperties(tbTypeTemplate,vo);
        return vo;
    }

    //修改

    @Override
    public void updateType(TypeVo vo) {
        TbTypeTemplate type = new TbTypeTemplate();
        BeanUtils.copyProperties(vo,type);
        tm.updateByPrimaryKeySelective(type);
    }

    //增加商品中的品牌

    @Override
    public TypeVo getitem(Long typeId) {
        TbTypeTemplate tbTypeTemplate = tm.selectByPrimaryKey(typeId);
        TypeVo vo = new TypeVo();
        BeanUtils.copyProperties(tbTypeTemplate,vo);
        return vo;
    }
}
